/**
 * @Project RSLsXWG
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Apr 29, 2011
 * @Createtime 3:38:48 PM
 */

package com.xwg.msg.utilities
{
	import com.xwg.msg.Emoticon;
	import com.xwg.msg.EmoticonSet;

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	import flashx.textLayout.elements.FlowElement;
	import flashx.textLayout.elements.InlineGraphicElement;
	import flashx.textLayout.elements.SpanElement;

	public class MessengerUtils extends EventDispatcher implements IEventDispatcher
	{
		//private var _emoticons:Array = [":\\*",":D"];
		private var _emoticons:EmoticonSet;

		private var patt:RegExp;

		public function MessengerUtils(emoticons:EmoticonSet, target:IEventDispatcher = null)
		{
			super(target);

			_emoticons = emoticons;
		}

		/**
		 * HTML format<br />
		 */
		public function stringToEmoString(input:String):String
		{
			var htmlString:String = input;

			/**
			 * input: type ">" >>> convert to "&gt;" - HTML safe<br />
			 * s.code = "&gt;" >>> patt = ">"<br />
			 * >>> must be replace all HTML safe to another character :D before add icon
			 */
			patt = /&gt;/gi;
			htmlString = htmlString.replace(patt, "@]@");

			for each (var s:Emoticon in _emoticons.emoticonSet)
			{
				/**
				 * i: ignore case
				 * g: global
				 */
				patt = new RegExp(s.code, "gi");

				/*
				switch (s)
				{
					case ":D":
						htmlString = htmlString.replace(patt, "<IMG WIDTH='24' HEIGHT='24' SRC='assets/emoticons/popo/BigGrin.png'/>");
						break;
					case ":\\*":
						htmlString = htmlString.replace(patt, "<IMG WIDTH='24' HEIGHT='24' SRC='assets/emoticons/popo/Kiss.png'/>");
						break;
					default:
						break;
				}
				*/

				htmlString = htmlString.replace(patt, "<IMG WIDTH='24' HEIGHT='24' SRC='" + s.imagePath + "'/>");
			}

			/**
			 * avoid type ">" but display "]"
			 */
			patt = /@]@/gi;
			htmlString = htmlString.replace(patt, "&gt;");

			return htmlString;
		}

		/**
		 * FOR TESTING PURPOSE ONLY!!!
		 */
		public function stringToFlowElements(input:String):Vector.<FlowElement>
		{
			var fES:Vector.<FlowElement> = new Vector.<FlowElement>();

			var ss:Array = input.split(_emoticons[0]);

			var preSE:SpanElement;
			var gE:InlineGraphicElement;
			var postSE:SpanElement;

			if (ss.length > 1)
			{
				/**
				 * loop from 1st element to element before last element!!!
				 */
				for (var i:uint = 0; i < ss.length - 1; i++)
				{
					if (ss[i] != '')
					{
						preSE = new SpanElement();
						preSE.text = ss[i];

						fES.push(preSE);
					}

					gE = new InlineGraphicElement();
					gE.source = "assets/Emoticon.png";

					fES.push(gE);
				}

				/**
				 * last element
				 */
				if (ss[ss.length - 1] != '')
				{
					postSE = new SpanElement();
					postSE.text = ss[ss.length - 1];

					fES.push(postSE);
				}
			}
			else
			{
				preSE = new SpanElement();
				preSE.text = ss[0];

				fES.push(preSE);
			}

			return fES;
		}

		private function stringToTMHCode(input:Array):Array
		{
			var array:Array = new Array();

			return array;
		}
	}
}
